package logic

import (
	"context"
	"course-admin/model"
	"course-admin/userrpc/internal/svc"
	"course-admin/userrpc/userrpc"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 收藏
func (l *CollectAddLogic) CollectAdd(in *userrpc.CollectAddRequest) (*userrpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line
	var findUser model.User

	if err := findUser.FindUserById(model.DB, in.UserId); err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	var findGoods model.Goods
	if err := findGoods.FindGoodsById(model.DB, in.GoodsId); err != nil {
		return nil, err
	}
	if findGoods.ID == 0 {
		return nil, errors.New("商品不存在")
	}
	var findCollect model.Collect
	if err := findCollect.FindCollectById(model.DB, in.UserId, in.GoodsId); err != nil {
		return nil, err
	}
	if findCollect.ID != 0 {
		return nil, errors.New("已收藏")
	}
	collect := model.Collect{
		UserId:  int(in.UserId),
		GoodsId: int(in.GoodsId),
	}

	if err := collect.CollectAdd(model.DB); err != nil {
		return nil, err
	}
	return &userrpc.CollectAddResponse{
		CollectId: int64(collect.ID),
	}, nil
}
